<?php
class Kbs_Application_Resource_Translate extends Zend_Application_Resource_ResourceAbstract
{
    protected $_translate;

    /**
     * To init the Translate
     *
     * @return Zend_Translate $translate
     */
    public function init()
    {
        if (null === $this->_translate) {
            $options = $this->getOptions();

            if (!isset($options['data'])) {
                throw new Zend_Application_Resource_Exception('No translation source data provided.');
            }

            $adapter = isset($options['adapter']) ? $options['adapter'] : Zend_Translate::AN_ARRAY;

            $this->getBootstrap()->bootstrap('Locale');
            $locale = $this->getBootstrap()->getResource('Locale');

            $data = '';
            if (isset($options['data']['directory'])) {
                $data = $options['data']['directory'] . $locale . $options['data']['fileExt'];
            }

            $translateOptions = isset($options['options']) ? $options['options'] : array();

            $translate = new Zend_Translate(
                $adapter, $data, $locale, $translateOptions
            );

            Zend_Registry::set($options['registry_key'], $translate);
            $this->_translate = $translate;
        }
        return $this->_translate;
    }
}